package final2107.day04;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class BlockingQueueDemo {
    public static void main(String[] args) {
        //创建初始容量为5的阻塞队列
        BlockingQueue<String> queue =
                new LinkedBlockingQueue<>(5);

        //生产者线程阻塞
        try {
            queue.put("hello1");
            System.out.println(queue);
            queue.put("hello2");
            System.out.println(queue);
            queue.put("hello3");
            System.out.println(queue);
            queue.put("hello4");
            System.out.println(queue);
            queue.put("hello5");
            System.out.println(queue);
            queue.put("hello6");
            System.out.println(queue);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //消费者线程阻塞
        Thread productor = new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    queue.put("hello");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        productor.start();


        //查看消费者现场是否阻塞
//        try {
//            String ele= queue.take();
//            System.out.println("取出:"+ele);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }
}
